<template>
  <div class="addressList">
    <CommonHeader title="地址列表">
      <template #left>
        <div>
          <Icon name="zuojiantou"></Icon>
          <span>返回</span>
        </div>
      </template>
    </CommonHeader>
    <!-- 地址列表 -->
    <div class="address_list" v-if="addressList.length > 0">
      <SwipeCell v-for="item in addressList" :key="item.id" :showImage="false" :title="item.username + item.userphone"
        :description="item.location">
        <template #left-checked>
          <div class="check" @click="handleCheck(item)" :class="{ active: item.ischeck }"></div>
        </template>
        <template #quantity>
          <span class="default" v-if="item.status === 1">默认</span>
          <Icon name="bianji" @click="handleEdit(item)"></Icon>
        </template>
        <template #right>
          <div class="cart_list_item_info_num_delete" @click="deleteCart(item.id)">
            删除
          </div>
        </template>
      </SwipeCell>
    </div>
    <!-- 地址列表为空时的提示 -->
    <div class="empty_tip" v-if="addressList.length === 0">
      <Icon name="wode"></Icon>
      <span>暂无地址,快去添加吧～</span>
    </div>
    <!-- 底部新增地址按钮 -->
    <div class="add_address_btn" @click="goAddnewaddress">
      <span>新增地址</span>
    </div>
  </div>
</template>

<script>
import CommonHeader from '@/components/common/publicHeader/index.vue';

import Icon from '@/components/common/Icon/index.vue';
import { getAddressListAPI } from '@/api/cart';
import SwipeCell from '@/components/common/swipeCell/index.vue'
import { deleteAddressAPI } from '@/api/cart';
import Message from '@/utils/message'
import showDialog from '@/utils/dialog';


export default {
  components: {
    CommonHeader,
    Icon,
    SwipeCell
  },
  data() {
    return {
      addressList: [],
    };
  },
  mounted() {
    this.getAddressList();
  },
  methods: {
    //跳转新增地址
    goAddnewaddress() {
      this.$router.push({
        name: 'Addnewaddress',
      });
    },
    //获取地址列表
    async getAddressList() {
      const { uid } = JSON.parse(localStorage.getItem('userinfo'));

      const res = await getAddressListAPI({
        uid,
      });
      const { list } = res;
      const selectedId = localStorage.getItem('selectedAddressId');
      // 如果res有list并且list有数据才map
      if (res.list && res.list.length) {
        this.addressList = list.map(item => ({
          ...item,
          ischeck: item.id === +selectedId, // 如果 id 匹配，就标记为选中
        }));
      } else {
        this.addressList = [];
      }
    },
    // 跳回订单确认页面
    handleCheck(item) {
      // 更新所有项
      this.addressList.forEach(i => (i.ischeck = false));
      item.ischeck = true;

      // 把选中的地址 id 存入 localStorage
      localStorage.setItem('selectedAddressId', item.id);
      // 把address保存到本地存储
      localStorage.setItem('authorInfo', JSON.stringify(item));
      this.$router.push({ name: 'orderConfirmation' });
    },
    // 编辑地址
    handleEdit(item) {
      this.$router.push({
        name: 'Editaddress',
      });
      // 把数据保存本地存储
      localStorage.setItem('editAddressData', JSON.stringify(item));
    },
    // 删除地址
    async deleteCart(id) {
      // 弹窗提示用户是否删除
      showDialog({
        title: '删除地址',
        content: '确定删除该地址吗？',
        onConfirm: async () => {
          // 发送删除请求
          const res = await deleteAddressAPI({ id });
          const { code, msg } = res;
          if (code === 200) {
            Message.success(msg);
            this.getAddressList();
          } else {
            Message.error(msg);
          }
        },
      })
    },
  
  },
};
</script>

<style lang="scss" scoped>
.check {
  width: 20px;
  height: 20px;
  border: 1px solid #ccc;
  border-radius: 50%;
  position: relative;

  &.active {
    background-color: #0076ff;
    border: 0;

    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -60%) rotate(45deg);
      width: 4px;
      height: 8px;
      border-right: 1px solid #fff;
      border-bottom: 1px solid #fff;
    }
  }
}

.empty_tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background-color: #fff;
  font-size: 14px;
  color: #999;
}

.address_list {
  padding: 10px;
  background-color: #fff;
  position: relative;

  .default {
    position: absolute;
    top: -30px;
    right: 30px;
    font-size: 12px;
    color: #fff;
    padding: 2px 5px;
    border-radius: 5px;
    background-color: #ff6140;
    white-space: nowrap;
  }

  .address_list_item {
    padding: 10px;
    border-bottom: 1px solid #ccc;
    display: flex;
    align-items: center;
    gap: 10px;

    .info {
      flex: 1;

      .author {
        font-size: 16px;
        font-weight: 600;
        color: #333;
        display: flex;
        align-items: center;
        white-space: nowrap;
        position: relative;



        .name {
          margin-right: 10px;
        }

        .phone {
          font-size: 14px;
          color: #999;
        }
      }

      .address {
        //最多两行
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        font-size: 14px;
        color: #999;
      }
    }
  }
}

.add_address_btn {
  position: fixed;
  bottom: 15px;
  left: 0;
  width: 100%;
  height: 50px;
  background-color: #ff6140;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  border-radius: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
}
</style>
